var selectedRow = '';
var isInit = true;

initLaydate();

function initLaydate() {
    laydate.render({
        elem: '#timeInterval'
        ,type: 'datetime'
        ,range: true //或 range: '~' 来自定义分割字符
        // ,value: dateFtt("yyyy-MM-dd hh:mm:ss", preDate) + ' - ' + dateFtt("yyyy-MM-dd hh:mm:ss", curDate)
        //js赋值重置失效，改为就是怕、页面赋值
        ,isInitValue: true //是否允许填充初始值，默认为 true
    });

    laydate.render({
        elem: '#timeInterval1'
        ,type: 'datetime'
        ,range: true //或 range: '~' 来自定义分割字符
        // ,value: dateFtt("yyyy-MM-dd hh:mm:ss", preDate) + ' - ' + dateFtt("yyyy-MM-dd hh:mm:ss", curDate)
        //js赋值重置失效，改为就是怕、页面赋值
        ,isInitValue: true //是否允许填充初始值，默认为 true
    });
}





$(function () {
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            openInfoPage();
        },
    };
    initTextSearch("#orgName",basePath + 'checkApp/checkDocTeam/getOrgList?t='+ (new Date()).getTime()+'&name=',['orgCode','orgName'],{'orgCode':'机构编码','orgName':'机构名称'},'orgCode','orgName',true,selectOrgValue,orgChange);

    //初始化操作
    if($("#status").val() == 'wait'){
        $("#unCheckDiv").show();
        $("#checkDiv").hide();
        $("#checkTypeDiv").hide();

        $("#detailBtn").hide();
        $("#checkBtn").show();

        $("#unCheckTable").show();
        $("#checkTable").hide();
    }else{
        $("#unCheckDiv").hide();
        $("#checkDiv").show();
        $("#checkTypeDiv").show();

        $("#detailBtn").show();
        $("#checkBtn").hide();

        $("#unCheckTable").hide();
        $("#checkTable").show();
    }

});

function doRefresh() {
    selectedRow = '';
    if($("#status").val() == 'wait'){
        $('#dataTable').bootstrapTable('refresh', {
            url: basePath + 'checkApp/checkDocTeam/pageFindCheckedOrgTeamData?t=' + new Date().getTime(),
        });
    }else{
        $('#dataTable1').bootstrapTable('refresh', {
            url: basePath + 'checkApp/checkDocTeam/pageFindCheckedOrgTeamPublishData?t=' + new Date().getTime(),
        });
    }
}

function getParams(params) {
    var docName = $('#docName').val();
    var checkType = $('#checkType').val();
    var teamName = $('#teamName').val();
    var orgCode = $('#orgCode').val();
    var result = $('#results').val();
    var status = $.trim($("#status").val());
    var gmtStart = '';
    var gmtEnd = '';
    var timeInterval = '';
    if(status == 'wait'){
        result = status;
        timeInterval = $("#timeInterval").val();
    }else{
        result = $.trim($("#results").val());
        timeInterval = $("#timeInterval1").val();
    }
    if(timeInterval){
        var dates = timeInterval.split(' - ');
        if(dates && dates.length >1){
            gmtStart = dates[0];
            gmtEnd = dates[1];
        }
    }
    if (isInit) {
        isInit = false;
        pageSize = searchInfo.pageSize ? searchInfo.pageSize : params.pageSize;
        pageNo = searchInfo.pageNo ? searchInfo.pageNo : params.pageNumber;
    }
    return {
        docName : docName,
        checkType : checkType,
        teamName : teamName,
        orgCode : orgCode,
        result : result,
        gmtStart : gmtStart,
        gmtEnd : gmtEnd,
        pageSize : pageSize,
        pageNo : pageNo
    };
}

function changeStatus(value){
    if(value == 'wait'){
        $("#unCheckDiv").show();
        $("#checkDiv").hide();
        $("#checkTypeDiv").hide();
        $("#detailBtn").hide();
        $("#checkBtn").show();

        $("#unCheckTable").show();
        $("#checkTable").hide();
    }else{
        $("#unCheckDiv").hide();
        $("#checkDiv").show();
        $("#checkTypeDiv").show();
        $("#detailBtn").show();
        $("#checkBtn").hide();

        $("#unCheckTable").hide();
        $("#checkTable").show();
    }
    doRefresh();
}

function doReset(formId){
    var status = $("#status").val();
    var formObj = $("#" + formId);
    if(formId && formObj){
        formObj.clearForm(true);
    }
    if(status != 'wait'){
        changeStatus('wait');
    }
}


function openCheckInfoPage() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("查看"));
        return;
    }
    var tableOptions = $('#dataTable1').bootstrapTable('getOptions', null);
    window.location.href = basePath + 'checkApp/checkDocTeam/openCheckOrgTeamPage?id=' + selectedRow.id  + '&type=info&page=2' + '&t=' + new Date().getTime() +
        '&hasOrg='+ defLogInfo.hasOrg + '&hasTeam='+ defLogInfo.hasTeam + '&hasApp='+ defLogInfo.hasApp +'&' + $('#searchForm').serialize() + '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

function openInfoPage() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + 'checkApp/checkDocTeam/openCheckOrgTeamPage?id=' + selectedRow.id + '&type=info&page=2' +
        '&hasOrg='+ defLogInfo.hasOrg + '&hasTeam='+ defLogInfo.hasTeam + '&hasApp='+ defLogInfo.hasApp + '&t=' + new Date().getTime() + '&' + $('#searchForm').serialize() + '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

function openCheckDocPage() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("审核"));
        return;
    }
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + 'checkApp/checkDocTeam/openCheckOrgTeamPage?id=' + selectedRow.id  + '&type=check&page=1' +
        '&hasOrg='+ defLogInfo.hasOrg + '&hasTeam='+ defLogInfo.hasTeam + '&hasApp='+ defLogInfo.hasApp +
        '&t=' + new Date().getTime() + '&' + $('#searchForm').serialize() + '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

function formatCheckType(value, row, index) {
    return convertMapCodeText(checkTypeMap,value);
}

function formatCheckResultType(value, row, index) {
    //return convertMapCodeText(checkResultTypeMap,value);
    if(checkResultTypeMap[value]){
        return checkResultTypeMap[value];
    }
    return checkResultTypeMap['unpass'];
}

function formatDeptName(value, row, index) {
    return row.cDeptName ? row.cDeptName : row.pDeptName;
}

function formatDocMediLevel(value, row, index) {
    return convertListCodeText(mediLevelList, value, "dictCode", "dictName");
}

$("#dataTable").on("click-row.bs.table",function(e,row,ele){
    $("#dataTable .success").removeClass("success");
    $(ele).addClass("success");
    selectedRow = row;
});

$("#dataTable1").on("click-row.bs.table",function(e,row,ele){
    $("#dataTable1 .success").removeClass("success");
    $(ele).addClass("success");
    selectedRow = row;
});

function orgChange() {
    //清空值
    $('#orgCode').val("");
}

function selectOrgValue(data) {
    //联动赋值
    if (data && data.orgCode && data.orgCode != '') {
        $('#orgCode').val(data.orgCode);
    }
}

function isvChange() {
    //清空值
    $('#isvPid').val("");
}

function selectIsvValue(data) {
    //联动赋值
    if (data && data.isvPid && data.isvPid != '') {
        $('#isvPid').val(data.isvPid);
    }
}